package com.zxt.web.controller;

import com.zxt.domain.Cart;
import com.zxt.domain.Goods;
import com.zxt.domain.User;
import com.zxt.service.CartService;
import com.zxt.service.GoodsService;
import com.zxt.service.GoodsTypeService;
import com.zxt.service.impl.CartServiceImpl;
import com.zxt.service.impl.GoodsServiceImpl;
import com.zxt.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * projectName:myXiaoMi
 * author:ZhangXiaoTeng
 * time:2021/12/6 19:04
 * description:
 */
@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet {

    //添加购物车
    //cartservlet?method=addCart&goodsId=1&number=1
    public String addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //1.获取前端数据
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if (StringUtils.isEmpty(goodsId)) {
            request.setAttribute("msg", "商品id不能为空");
            return "/message.jsp";
        }
        int num = 1;
        if (!StringUtils.isEmpty(number)) {
            try {
                num = Integer.parseInt(number);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        try {
            //2.调用业务逻辑
            int goods_id = Integer.parseInt(goodsId);
            CartService cartService = new CartServiceImpl();
            //根据商品id查找单价
            GoodsService goodsService = new GoodsServiceImpl();
            Goods goods = goodsService.findById(goods_id);

            //加查看购物车是否有商品
            Cart cart=cartService.find(user.getId(),goods_id);
            //如果存在更新数量num
            if (cart!=null){
            cart.setNum(cart.getNum()+num);
            cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
            cartService.modfiy(cart);
            }else {
                //如果不存在 创建对象添加数据库
                cart = new Cart(user.getId(),goods_id,num,goods.getPrice().multiply(new BigDecimal(num)));
                cartService.add(cart);
            }
            //3.转发重定向
            return "redirect:/cartSuccess.jsp";
        } catch (Exception e) {
            request.setAttribute("msg", "添加购物车失败" + e.getMessage());
            return "/message.jsp";
        }

    }

    //cartservlet?method=getCart
    //获取购物车信息
    public String getCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }

        try {
            //2.调用业务逻辑
            CartService cartService = new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            //3.转发重定向
            request.setAttribute("cartList",cartList);
            return "/cart.jsp";
        } catch (Exception e) {
            request.setAttribute("msg", "查看购物车失败" + e.getMessage());
            return "/message.jsp";
        }
    }


    //AJAX查看购物车数量
    //cartservlet?method=addCartAjax&goodsId="+pid+"&number=1"
    public String addCartAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //获取前端数据
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if (StringUtils.isEmpty(goodsId)) {
            request.setAttribute("msg", "商品id不能为空");
            return "/message.jsp";
        }

        if (StringUtils.isEmpty(goodsId)) {
            request.setAttribute("msg", "商品数量不能为空");
            return "/message.jsp";
        }

        try {
            //2.调用业务逻辑
            Integer num = Integer.parseInt(number);
            Integer goods_id= Integer.parseInt(goodsId);
            CartService cartService = new CartServiceImpl();
            if (num==0){    //删除商品
                cartService.remove(user.getId(),Integer.parseInt(goodsId));
            }else if (num==1||num==-1){  //更新数据
                Cart cart = cartService.find(user.getId(),goods_id);
                if (cart !=null){
                    GoodsService goodsService=new GoodsServiceImpl();
                    Goods goods = goodsService.findById(goods_id);
                    cart.setNum(cart.getNum()+num);
                    cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                    cartService.modfiy(cart);
                }
            }
            //3.转发重定向
        } catch (Exception e) {
            request.setAttribute("msg", "AJAX操作购物车失败" + e.getMessage());
            return "/message.jsp";
        }
         return null;

    }


    //清空购物车
    public String clearCartAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        try {
            //2.调用业务逻辑
            CartService cartService = new CartServiceImpl();
            cartService.clear(user.getId());
        } catch (Exception e) {
            request.setAttribute("msg", "AJAX清空购物车失败" + e.getMessage());
            return "/message.jsp";
        }
        return null;
    }
}

